//
//  CZNetworkTools.swift
//  WeiBo001
//
//  Created by ZhiQiang Li on 16/7/22.
//  Copyright © 2016年 ZhiQiang Li. All rights reserved.
//

import UIKit
import AFNetworking

enum CZNetworkToolsType: String {
    case GET = "GET"
    case POST = "POST"
}

class CZNetworkTools: AFHTTPSessionManager {
    // 全局访问点
    static let sharedTools:  CZNetworkTools = {
        let tools =  CZNetworkTools()
        // 指定反序列化格式
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools

}()

    func request(type: CZNetworkToolsType, urlString: String, parameters: AnyObject?, success: (response: AnyObject?)->(), failure: (error: NSError)->()){
        // get请求
        if type == .GET {
            GET(urlString, parameters: parameters, progress: nil, success: { (_, res) -> Void in
                success(response: res)
                }, failure: { (_, err) -> Void in
                    failure(error: err)
            })
        }else {
            // post 请求
            POST(urlString, parameters: parameters, progress: nil, success: { (_, res) -> Void in
                success(response: res)
                }, failure: { (_, err) -> Void in
                    failure(error: err)
            })
        }
        
    }
    
}
extension CZNetworkTools{
    // 请求userAccount
    func oauthLoadUserAccount(code: String, success: (response: AnyObject?)->(), failure: (error: NSError)->()){
        // 请求urlString
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id": APPKEY,
            "client_secret": APPSECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": APPREDIRECT_URI
        ]
        request(CZNetworkToolsType.POST, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    // 请求用户信息数据
    func oauthLoadUserInfo(userAccount:CZModel, success: (response: AnyObject?)->(), failure: (error: NSError)->()){
        // 请求的urlString
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 请求参数
        let params = [
            "access_token": userAccount.access_token!,
            "uid": userAccount.uid!
        ]
        request(CZNetworkToolsType.GET, urlString: urlString, parameters: params, success: success, failure: failure)
    }
}
//请求界面数据
extension CZNetworkTools {
    
    // 请求userAccount
    func oauthLoadUesrAccount(code: String, success: (response: AnyObject?)->(), failure: (error: NSError)->()){
        // 请求urlString
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id": APPKEY,
            "client_secret": APPSECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": APPREDIRECT_URI
        ]
        request(CZNetworkToolsType.POST, urlString: urlString, parameters: params, success: success, failure: failure)
    
}
    
    // 请求用户信息数据
    func oauthLoadUserIofo(userAccount:CZModel, success: (response: AnyObject?)->(), failure: (error: NSError)->()){
        // 请求的urlString
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 请求参数
        let params = [
            "access_token": userAccount.access_token!,
            "uid": userAccount.uid!
        ]
        request(CZNetworkToolsType.GET, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
}
    extension CZNetworkTools{
        func homeLoadData(sinceId: Int64, maxId: Int64, success: (response: AnyObject?)->(), failure: (error: NSError)->()){
            // 请求urlString
            let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
            // 请求参数
            let params = [
                "access_token": CZUserAccountViewModel.sharedTools.accessToken!,
                "since_id":"\(sinceId)",
                "max_id":"\(maxId)"
            ]
            
            
            request(CZNetworkToolsType.GET, urlString: urlString, parameters: params, success: success, failure: failure)
            
        }
}


